package com.hly.tool.login;

import com.matrixone.apps.domain.util.FrameworkException;
import com.matrixone.apps.domain.util.MqlUtil;
import com.matrixone.apps.framework.ui.UIUtil;
import matrix.db.Context;
import matrix.db.JPOSupport;


import java.util.*;

/**
 * @author ：Jack Hao
 * @date ：Created in 2021/8/10 9:08
 * @description：
 */
public class Login
{
    /**
     * 登录3DE
     *
     * @return
     */
    public static Map login(String host, String role, String user, String password)
    {
        Map result = new HashMap();
        Context context = null;
        try
        {
            //10次 尝试登录
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    context = ContextLoginUtil.getCasAdminContext(host, role, user, password);
                    break;
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            if (context == null || !context.isConnected())
            {
                result.put("msg", "FAIL");
                result.put("context", null);
            }
            result.put("msg", "OK");
            result.put("context", context);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * VPLMProjectLeader
     * <p>
     * 登录功能，如果用户上下文中有角色 就是把第一个设置为 安全上下文，
     * 如果没有 角色  就不设置 安全上下文
     *
     * @param host
     * @param user
     * @param password
     */
    public static Context loginDefaultRole(String host, String user, String password) throws Exception
    {
        String passportTicket = Passport.getTicket(host, user, password);

        Context context = new Context(host + passportTicket);

        context.setUser(user);
        context.setLocale(new Locale("en-US"));
        context.connect();
        JPOSupport.registerThread(context);

        Vector roleVec = getPersonRole(context, user);

        String role = "";
        if (roleVec.size() > 0)
        {
            role = (String) roleVec.get(0);
        }

        if (UIUtil.isNotNullAndNotEmpty(role))
        {
            Map login = login(host, role, user, password);
            context = (Context) login.get("context");
        }

        return context;
    }

    /**
     * VPLMProjectLeader
     * <p>
     * 登录功能，如果用户上下文中有 VPLMProjectLeader 角色 就是设置为 安全上下文，
     * 如果没有 角色 VPLMProjectLeader 就不设置 安全上下文
     *
     * @param host
     * @param user
     * @param password
     */
    public static Context loginDefaultRoleProjectLeader(String host, String user, String password) throws Exception
    {
        String passportTicket = Passport.getTicket(host, user, password);
        Context context = new Context(host + passportTicket);
        context.setUser(user);
        context.setLocale(new Locale("en-US"));
        context.setRole("");
        context.connect();
        JPOSupport.registerThread(context);
        Vector roleVec = getPersonRole(context, user);
        String role = "";

        for (int i = 0; i < roleVec.size(); ++i)
        {
            String role_i = (String) roleVec.get(i);
            if (role_i.contains("VPLMProjectLeader"))
            {
                role = role_i;
                break;
            }
        }
        if (UIUtil.isNotNullAndNotEmpty(role))
        {
            Map login = Login.login(host, role, user, password);
            context = (Context) login.get("context");
        }
        return context;
    }


    /**
     * 获取用户的role
     *
     * @param context
     * @param person
     * @return
     * @throws FrameworkException
     */
    private static Vector getSortPersonRole(Context context, String person) throws FrameworkException
    {
        Vector<String> vec = new Vector<>();

        String res = MqlUtil.mqlCommand(context, "list person '" + person + "' select assignment dump");

        String[] values = res.split(",");
        for (int i = 0; i < values.length; i++)
        {
            if (values[i].startsWith("ctx::"))
            {
//                vec.add(values[i].substring(5));
                vec.add(values[i]);
            }
        }

        Collections.sort(vec);

        return vec;
    }

    /**
     * 获取用户的role
     *
     * @param context
     * @param person
     * @return
     * @throws FrameworkException
     */
    private static Vector getPersonRole(Context context, String person) throws FrameworkException
    {
        Vector<String> vec = new Vector<>();

        String res = MqlUtil.mqlCommand(context, "list person '" + person + "' select assignment dump");

        String[] values = res.split(",");
        for (int i = 0; i < values.length; i++)
        {
            if (values[i].startsWith("ctx::"))
            {
//                vec.add(values[i].substring(5));
                vec.add(values[i]);
            }
        }
        return vec;
    }

}
